import { GameConfig, GameEvent, comp, lib } from "../../Const";
import { loadManifestAsync } from "../../CreateJSHelper";
import LoadingView from "../../view/LoadingView";
import BaseController from "../BaseController";
import Main from '../../Main';
import cjs=createjs
import createjs from "createjs-ts";

export default class LoadController extends BaseController {
    private load_view:LoadingView;
    
    start(): void {
        this.message.once(GameEvent.GAME_LOADING,()=>{
            this.load_view=new LoadingView()
            this.load_view.setSize(GameConfig.stageWidth,GameConfig.stageHeight)
            this.layer.game_layer.addChild(this.load_view)
            //
            loadManifestAsync(lib.properties.manifest,'./assets/main/',null,(evt:cjs.Event)=>{
                console.log(evt.item.src)
                const images=comp.getImages();	
	            if (evt && (evt.item.type == "image")) { images[evt.item.id] = evt.result; }
            },(e:cjs.Event)=>{
                const evt=e as cjs.ProgressEvent
                //console.log(evt.progress)
                this.load_view.progress=evt.progress
            },cjs.Sound).then((loader:cjs.LoadQueue)=>{
                //
	            const ss=comp.getSpriteSheet();
	            const queue = loader;
                var ssMetadata = lib.ssMetadata;
                for(let i=0; i<ssMetadata.length; i++) {
                    ss[ssMetadata[i].name] = new createjs.SpriteSheet( {"images": [queue.getResult(ssMetadata[i].name)], "frames": ssMetadata[i].frames} )
                }
                //new lib.Main();
                this.message.send(GameEvent.GAME_START)
            })
        })
        //
    }
}